﻿#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <tchar.h>

/// @brief 打印指定名称的环境变量
/// @param name 环境变量名
void PrintEnviromentVariable(PCTSTR name) {
	PTSTR value = NULL;
	DWORD res = GetEnvironmentVariable(name, value, 0);
	if (res != 0) {
		DWORD size = res * sizeof(TCHAR);
		value = (PTSTR)malloc(size);
		GetEnvironmentVariable(name, value, size);
		_tprintf(_T("%s=%s\n"), name, value);
		free(value);
	}
	else
		_tprintf(_T("'%s'=<unknown value>\n"), name);
}

int _tmain() {
	// 打印环境变量名
	wprintf(L"环境变量：\n");
	PrintEnviromentVariable(_T("env1"));
	PrintEnviromentVariable(_T("env2"));

	// 打印命令行
	wprintf(L"\n命令行：\n");
	int nNumArgv;
	PWSTR* ppArgv = CommandLineToArgvW(GetCommandLineW(), &nNumArgv);
	for (size_t i = 0; i < nNumArgv; i++)
		wprintf(L"argv[%d]=%s\n", i, ppArgv[i]);

	system("pause");
	return 0;
}